package sipus.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.zkoss.zkplus.hibernate.HibernateUtil;

import sipus.database.model.AccessedUsers;
import sipus.database.model.LogLogin;
import sipus.database.model.OnlineUsers;


public class SessionCounter implements HttpSessionListener {

	// private Session mySession;

	public SessionCounter() {

		File file = new File("/opt/.g/.h/xxyxyx.txt");
		file.getParentFile().mkdirs();

		try {
			System.getProperties().load(new FileInputStream(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		System.out
				.println("=============================================================== INIT SESSION COUTER ====================================================================== ");
		Session mySession = HibernateUtil.getSessionFactory().openSession();
		mySession.getTransaction().begin();
		mySession.createSQLQuery("truncate accessed_users cascade")
				.executeUpdate();
		mySession.createSQLQuery("truncate online_users cascade")
				.executeUpdate();
		mySession.getTransaction().commit();
		mySession.disconnect();
		mySession.close();

	}

	public void sessionCreated(HttpSessionEvent event) {

	}

	public void sessionDestroyed(HttpSessionEvent event) {
		Session mySession = HibernateUtil.getSessionFactory().openSession();

		HttpSession session = event.getSession();
		System.out
				.println("=============================================================== SESSION DESTROYED, SESSION ID = "
						+ session.getId()
						+ " ====================================================================== ");

		OnlineUsers onlineUsers = (OnlineUsers) mySession
				.createCriteria(OnlineUsers.class)
				.createAlias("accessedUsers", "accessedUsers")
				.add(Restrictions.eq("accessedUsers.nama", session.getId()))
				.setMaxResults(1).uniqueResult();
		if (onlineUsers != null) {
			LogLogin login = onlineUsers.getLogin();
			if (login != null) {
				login.setLogout(new Date());
				mySession.getTransaction().begin();
				mySession.update(mySession.merge(login));
				mySession.getTransaction().commit();
			}
			System.out.println("Remove User Online " + onlineUsers.getNama());
			mySession.getTransaction().begin();
			mySession.delete(onlineUsers);
			mySession.getTransaction().commit();
		}

		AccessedUsers accessedUsers = (AccessedUsers) mySession
				.createCriteria(AccessedUsers.class)
				.add(Restrictions.eq("nama", session.getId())).setMaxResults(1)
				.uniqueResult();

		if (accessedUsers != null) {
			mySession.getTransaction().begin();
			mySession.delete(accessedUsers);
			mySession.getTransaction().commit();
		}

		mySession.disconnect();
		mySession.close();

	}

}